<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2014/10/9
 * Time: 11:59
 * 客户问卷表
 * 一个问卷可以属于多个客户，所以一个客户和一个问卷确定这个客户的问卷信息
 */
class NetworkQuestion extends CActiveRecord {

    public static function model($className = __CLASS__) {
            return parent::model($className);
    }

    public function tableName() {
            return 'network_question';
    }
	
    const NETWORK_QUESTION_DELETE = 0; //网点问卷状态：删除
    const NETWORK_QUESTION_NORMAL = 1;  //网点问卷状态：正常
    const NETWORK_QUESTION_PAUSE = -1;  //网点问卷状态：暂停
    const NETWORK_QUESTION_COMPLETE = 2;//网点状态：完成

    /*
     * 获取网点问卷状态
     */
    public static function networkQuestionStatus(){
            return array(
                self::NETWORK_QUESTION_DELETE =>  '删除',
                self::NETWORK_QUESTION_NORMAL => '正常',
                self::NETWORK_QUESTION_PAUSE =>  '暂停',
                self::NETWORK_QUESTION_COMPLETE => '完成',
            );
    }
	
    //结点状态---操作使用
    public static $audit = array(
        0 => '未审核',
        //1 => '提出异议',
        2 => '向上提交',
        3 => '向下打回',
        4 => '提交'
    );
    public static $audit_show = array(
        0 => '未审核',
        //1 => '已提出异议',
        2 => '已向上提交',
        3 => '已向下打回',
        4 => '已提交'
    );
    //网点等级拥有的结点状态
    public static $level_auth = array(
        1 => array(3,4),
        2 => array(2,3),
        3 => array(2, 3),
        4 => array(2)
    );
    /**
     * 根据网点id和问卷id确定，此网点的问卷
     */
    public function getByNetworkQuestion($network_id, $question_id){
        if ($info = $this->find('network_id=:network_id and question_id=:question_id', array(':network_id'=> $network_id, ':question_id' => $question_id))){
            return $info->attributes;
        }
        return $info;
    }
    /**
     * 获取网点问卷
     */
    public function getByNetwork($network_id){
        $data = $this->findAll(array('network_id' => $network_id));
        if ($data) {
            foreach ($data as $key => $val) {
                
            }
            
        }
    }
    
    /**
     * 根据问卷ID添加问卷和网点的对应关系
     * @param type $questionId
     * @param array $inDatas
     * @return boolean
     */
    public function addNetworkQuestion($projectId, $questionId, array $inDatas){
        if(!$projectId || !$questionId || empty($inDatas)){
                return false;
        }

        # 添加之前，先删除表中所以的关于此问卷的网点
        $this->deleteAll('project_id=:project_id and question_id=:question_id', array(':project_id' => $projectId, ':question_id'=>$questionId));
        $return = true;
        foreach ($inDatas as $key => $value) {
                $model = new NetworkQuestion();
                foreach ($value as $i => $v) {
                        $model->$i = $v;
                }
                $return &= $model->save();
        }

        return $return;
    }
    /**
     * 得到网点的项目
     */
    public function getNetworkProject($network_id){
        $data = $this->findAll('network_id='.$network_id);
        if ($data) {
            $project = new Project();
            $result = array();
            foreach ($data as $key => $val) {
                $info = $project->findByPk($val->project_id);
                $result[$val->project_id] = $info->attributes;
            }
            return $result;
        }
        return $data;
    }
	
	/**
	 * 根据项目ID，问卷ID，问卷类型获取所有网点
	 * @param type $questionId
	 * @param type $projectId
	 * @return type
	 */
	public function getQuestionNetwork($questionId, $projectId){
		if (!$questionId && !$projectId) {
			return array();
		}
		$networks = $this->findAll('question_id='.$questionId.' and project_id='.$projectId);
		$networdIds = array();
		foreach ($networks as $value) {
			$networdIds[$value['network_id']] = $value['nq_id'];
		}
		$return = array();
		foreach (NetWork::model()->findAllByPk(array_keys($networdIds)) as $data) {
			$data = $data->attributes;
			$return[$data['network_num']] = $data;
			$return[$data['network_num']]['nq_id'] = $networdIds[$data['network_id']];
		}
		return $return;
	}
	
	/**
	 * 根据项目ID，问卷ID，判断当前问卷所有的网点级别，递归获取上级网点
	 * @param type $questionId	问卷ID
	 * @param type $projectId	项目ID
	 * @param type $networkIds	网点ID，如果存在，直接获取
	 * @param boolean $level	所有的网点级别
	 * @return boolean
	 */
	public function getAllLevelNetwork($questionId, $projectId, $networkIds = array(), &$level = array()){
		if (!$questionId && !$projectId) {
			return array();
		}
		$network= array();
		$up_network_ids = array();
		
		# 如果存在网点ID，方便递归调用使用
		if (empty($networkIds)) {
			$network = $this->getQuestionNetwork($questionId, $projectId);
		}else{
			$network = Network::model()->findAll('network_id in('.join(',', $networkIds).')');
		}
		
		# 查询网点
		if (!empty($network)) {
			foreach ($network as $val) {
				$level[$val['auth_grade']] = true;
				if ($val['up_network_id']) {
					$up_network_ids[$val['up_network_id']] = $val['up_network_id'];
				}
			}
		}
		
		# 如果存在上级网点
		if (!empty($up_network_ids)) {
			$this->getAllLevelNetwork($questionId, $projectId, $up_network_ids, $level);
		}
		
		return $level;
	}
}
